package com.ruoyi.project.explorer.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.framework.config.RuoYiConfig;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.explorer.domain.ExplorAddParam;
import com.ruoyi.project.explorer.domain.Explorer;
import com.ruoyi.project.explorer.domain.ExplorerDto;
import com.ruoyi.project.explorer.mapper.ExplorerMapper;
import com.ruoyi.project.explorer.service.IExplorerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.List;

import static com.ruoyi.common.utils.security.ShiroUtils.getUserId;
import static com.ruoyi.framework.web.domain.AjaxResult.error;

/**
 * 资源管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2019-11-29
 */
@Service
public class ExplorerServiceImpl implements IExplorerService
{
    @Autowired
    private ExplorerMapper explorerMapper;

    /**
     * 查询资源管理
     * 
     * @param id 资源管理ID
     * @return 资源管理
     */
    @Override
    public Explorer selectExplorerById(Long id)
    {
        return explorerMapper.selectExplorerById(id);
    }

    /**
     * 查询资源管理列表
     * 
     * @param explorer 资源管理
     * @return 资源管理
     */
    @Override
    public List<ExplorerDto> selectExplorerList(Explorer explorer)
    {
        return explorerMapper.selectExplorerList(explorer);
    }

    /**
     * 新增资源管理
     * 
     * @param explorAddParam 资源管理
     * @return 结果
     */
    @Override
    public AjaxResult insertExplorer(ExplorAddParam explorAddParam)
    {
        String upload=null;
        try
        {
            if (!explorAddParam.getFile().isEmpty())
            {
                upload= FileUploadUtils.upload(RuoYiConfig.getUploadPath(), explorAddParam.getFile());
                explorAddParam.setPath(upload);
                explorAddParam.setUserId(getUserId());
                if (explorerMapper.insertExplor(explorAddParam) > 0)
                {
                    return new AjaxResult(AjaxResult.Type.SUCCESS,"添加成功！");
                }
            }
            return error();
        }
        catch (Exception e)
        {
            String path=RuoYiConfig.getUploadPath()+upload.substring(upload.indexOf("upload")+6,upload.length());
            File file2=new File(path);
            if(file2.exists()){
                Boolean result=file2.delete();
            }
            return new AjaxResult(AjaxResult.Type.ERROR,"添加失败！");
//            return error(e.getMessage());
        }
    }

    /**
     * 修改资源管理
     * 
     * @param explorer 资源管理
     * @return 结果
     */
    @Override
    public int updateExplorer(Explorer explorer)
    {
        explorer.setUpdateTime(DateUtils.getNowDate());
        String upload=null;
        try
        {
            if (!explorer.getFile().isEmpty())
            {
                upload= FileUploadUtils.upload(RuoYiConfig.getUploadPath(), explorer.getFile());
                explorer.setPath(upload);
                explorer.setUserId(getUserId());
            }
        }
        catch (Exception e)
        {
            String path=RuoYiConfig.getUploadPath()+upload.substring(upload.indexOf("upload")+6,upload.length());
            File file2=new File(path);
            if(file2.exists()){
                Boolean result=file2.delete();
            }
        }
        return explorerMapper.updateExplorer(explorer);
    }

    /**
     * 删除资源管理对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteExplorerByIds(String ids)
    {
        return explorerMapper.deleteExplorerByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除资源管理信息
     * 
     * @param id 资源管理ID
     * @return 结果
     */
    @Override
    public int deleteExplorerById(Long id)
    {
        return explorerMapper.deleteExplorerById(id);
    }
}
